package com.gx.smart.construction.ui.elevator.fragment;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;

import com.blankj.utilcode.util.ToastUtils;
import com.gx.smart.construction.R;
import com.gx.smart.construction.databinding.ElevatorControlFragmentBinding;
import com.gx.smart.construction.ui.common.fragment.CommonSelectRoomFragment;
import com.gx.smart.construction.ui.common.model.ElevatorName;
import com.gx.smart.construction.ui.common.model.UnitName;
import com.gx.smart.construction.ui.elevator.viewmodel.ElevatorControlViewModel;
import com.gx.smart.lib.event.LoadingMessageEvent;
import com.gx.smart.lib.widget.dialog.CustomAlertDialog;
import com.gx.wisestone.constructionappgrpclib.grpc.liftinfo.ConstructionAppLiftInfoDto;
import com.mylhyl.circledialog.CircleDialog;
import com.orhanobut.logger.Logger;
import com.yzq.zxinglibrary.encode.CodeCreator;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

public class ElevatorControlFragment extends CommonSelectRoomFragment<ElevatorControlViewModel> {
    private List<ElevatorName> elevatorNameItems = new ArrayList<>();
    private String lifeId;
    private ElevatorControlFragmentBinding binding;
    private final Observer<List<ConstructionAppLiftInfoDto>> lifeObserver = new Observer<List<ConstructionAppLiftInfoDto>>() {
        @Override
        public void onChanged(List<ConstructionAppLiftInfoDto> list) {
            EventBus.getDefault().post(new LoadingMessageEvent(false));
            elevatorNameItems.clear();
            if (list == null || list.isEmpty()) {
                ToastUtils.showLong("电梯为空");
                return;
            }
            for (ConstructionAppLiftInfoDto item : list) {
                ElevatorName item1 = new ElevatorName();
                item1.setContent(item.getLiftName());
                item1.setId(String.valueOf(item.getLiftId()));
                elevatorNameItems.add(item1);
            }

            new CircleDialog.Builder()
                    .setItems(elevatorNameItems, (parent, view, position, id) -> {
                        ElevatorName item = elevatorNameItems.get(position);
                        binding.selectElevator.setText(item.getContent());
                        lifeId = item.getId();
                        return true;
                    })
                    .setNegative("取消", null)
                    .show(requireActivity().getSupportFragmentManager());
        }

    };

    private final Observer<String> qrcodeObserver = s -> {
        EventBus.getDefault().post(new LoadingMessageEvent(false));
        if (!TextUtils.isEmpty(s)) {
            Bitmap qrCodeBitmap = CodeCreator.createQRCode(s, 600, 600, null);
            CustomAlertDialog.with((AppCompatActivity) getActivity())
                    .setCancelable(false)
                    .setBitmap(qrCodeBitmap)
                    .show();
        } else {
            ToastUtils.showLong("二维码信息为空!");
        }
    };

    @Override
    protected Class<ElevatorControlViewModel> onBindViewModel() {
        return ElevatorControlViewModel.class;
    }

    @Override
    public void initContentView(ViewGroup root) {
        binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.elevator_control__fragment, root, true);
        binding.setFragment(this);
        binding.setSelectRoomViewModel(viewModel);
        binding.setLifecycleOwner(getViewLifecycleOwner());
    }

    @Override
    public int onBindLayout() {
        return R.layout.elevator_control__fragment;
    }

    @Override
    public String getToolbarTitle() {
        return "";
    }

    @Override
    public boolean enableToolbar() {
        return false;
    }

    @Override
    protected void initObserver() {
        viewModel.error.observe(this, result -> {
            if (result) {
                EventBus.getDefault().post(new LoadingMessageEvent(false));
            }
        });
    }

    @SuppressWarnings("unused")
    public void selectArea(View v) {
        selectArea();
    }

    @SuppressWarnings("unused")
    public void selectBuilding(View v) {
        selectBuilding();
    }

    @SuppressWarnings("unused")
    public void selectUnit(View v) {
        selectUnit();
    }

    @SuppressWarnings("unused")
    public void selectElevator(View v) {
        UnitName unitName = viewModel.getUnitName().getValue();
        if (unitName == null) {
            ToastUtils.showLong("请先选择单元");
            return;
        }
        EventBus.getDefault().post(new LoadingMessageEvent(true));
        viewModel.liftList(1, unitName.getId()).observe(getViewLifecycleOwner(), lifeObserver);
    }

    @SuppressWarnings("unused")
    public void createQRCode(View v) {
        createQRCode();
    }

    private void createQRCode() {
        String areaName = binding.selectArea.getText().toString();
        String buildingName = binding.selectBuilding.getText().toString();
        String unitName = binding.selectUnit.getText().toString();
        String elevatorName = binding.selectElevator.getText().toString();
        if (TextUtils.isEmpty(areaName)) {
            ToastUtils.showLong("请选择分区");
            return;
        }
        if (TextUtils.isEmpty(buildingName)) {
            ToastUtils.showLong("请选择楼栋");
            return;
        }
        if (TextUtils.isEmpty(unitName)) {
            ToastUtils.showLong("请选择单元");
            return;
        }
        if (TextUtils.isEmpty(elevatorName)) {
            ToastUtils.showLong("请选择电梯");
            return;
        }

        EventBus.getDefault().post(new LoadingMessageEvent(true));
        viewModel.createQRCode(lifeId).observe(getViewLifecycleOwner(), qrcodeObserver);
    }


}